//
//  LXSelectView.m
//  cat
//
//  Created by imera on 16/3/15.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXSelectView.h"
#import "LXImageOnTopButton.h"
#import "UIColor+LX.h"

#define bili 0.22

@interface LXSelectView()
/** 标题 */
@property (nonatomic, copy) NSString *title;
/** 选择图片 */
@property (nonatomic, strong) NSArray *imageArr;
/** 选择图片名字 */
@property (nonatomic, strong) NSArray *imageTitleArr;
/** 底部btn的标题 */
@property (nonatomic, copy) NSString *bottomBtnTitle;
/** 顶部标题 */
@property (nonatomic, weak) UILabel *titleLabel;
/** 顶部标题分割线 */
@property (nonatomic, weak) UIView *titleLabelDivider;
/** 选择按钮视图 */
@property (nonatomic, weak) UIView *selectBtnView;
/** 选择按钮数组 */
@property (nonatomic, strong) NSArray *btnArr;
/** 选择按钮数组分割线 */
@property (nonatomic, weak) UIView *btnArrDivider;
/** 底部按钮 */
@property (nonatomic, weak) UIButton *bottomBtn;

/** 当前选择的按钮 */
@property (nonatomic, weak) UIButton *currentBtn;
@end

@implementation LXSelectView

+ (instancetype)initWithTitle:(NSString *)title imageArr:(NSArray *)imageArr imageTitleArr:(NSArray *)imageTitleArr bottomBtnTitle:(NSString *)bottomBtnTitle
{
    return [[self alloc]initWithTitle:title imageArr:imageArr imageTitleArr:imageTitleArr bottomBtnTitle:bottomBtnTitle];
}

- (instancetype)initWithTitle:(NSString *)title imageArr:(NSArray *)imageArr imageTitleArr:(NSArray *)imageTitleArr bottomBtnTitle:(NSString *)bottomBtnTitle
{
    self.title = title;
    self.imageArr = imageArr;
    self.imageTitleArr = imageTitleArr;
    self.bottomBtnTitle = bottomBtnTitle;
    return [self init];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor whiteColor];
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews
{
    /** 顶部标题 */
    UILabel *titleLabel = [[UILabel alloc]init];
    self.titleLabel = titleLabel;
    [self addSubview:titleLabel];
    
    /** 顶部标题分割线 */
    UIView *titleLabelDivider = [[UIView alloc]init];
    self.titleLabelDivider = titleLabelDivider;
    [self addSubview:titleLabelDivider];
    
    /** 选择按钮视图 */
    UIView *selectBtnView = [[UIView alloc]init];
    self.selectBtnView = selectBtnView;
    [self addSubview:selectBtnView];
    
    /** 选择按钮数组 */
    NSMutableArray *btnArr = [NSMutableArray array];
    for(int i=0;i<self.imageArr.count;i++){
        LXImageOnTopButton *btn = [[LXImageOnTopButton alloc]init];
        [selectBtnView addSubview:btn];
        [btnArr addObject:btn];
    }
    self.btnArr = btnArr;
    
    /** 选择按钮数组分割线 */
    UIView *btnArrDivider = [[UIView alloc]init];
    self.btnArrDivider = btnArrDivider;
    [self addSubview:btnArrDivider];
    
    /** 底部按钮 */
    UIButton *bottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.bottomBtn = bottomBtn;
    [self addSubview:bottomBtn];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    /** 顶部标题 */
    UILabel *titleLabel = self.titleLabel;
    titleLabel.text = self.title;
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    CGFloat titleLabelX = 0;
    CGFloat titleLabelY = 0;
    CGFloat titleLabelW = self.frame.size.width;
    CGFloat titleLabelH = self.frame.size.height * bili;
    titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
    
    /** 顶部标题分割线 */
    UIView *titleLabelDivider = self.titleLabelDivider;
    titleLabelDivider.alpha = 0.5;
    titleLabelDivider.backgroundColor = [UIColor grayColor];
    titleLabelDivider.frame = CGRectMake(0, titleLabelH, self.frame.size.width, 1);
    
    /** 选择按钮视图 */
    UIView *selectBtnView = self.selectBtnView;
    
    CGFloat selectBtnViewX = 0;
    CGFloat selectBtnViewY = CGRectGetMaxY(titleLabelDivider.frame);
    CGFloat selectBtnViewW = self.frame.size.width;
    CGFloat selectBtnViewH = self.frame.size.height * (1 - bili * 2);
    selectBtnView.frame = CGRectMake(selectBtnViewX, selectBtnViewY, selectBtnViewW, selectBtnViewH);
    
    /** 选择按钮数组 */
    CGFloat btnH = 85;
    CGFloat btnW = 0;
    if(self.isSelectItemCircle){ //  圆形按钮
        btnW = btnH;
    }else{
        btnW = btnH * LXImageOnTopButtonBili;
    }
    CGFloat btnY = (selectBtnView.frame.size.height - btnH) / 2;
    CGFloat margin = (self.frame.size.width - btnW * self.btnArr.count) / (self.btnArr.count + 1);
    for(int i=0;i<self.imageArr.count;i++){
        LXImageOnTopButton *btn = self.btnArr[i];
        [btn setImage:[UIImage imageNamed:self.imageArr[i]] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitle:self.imageTitleArr[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(itemSelected:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i;
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        CGFloat btnX = (btnW + margin) * i + margin;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        
        if (self.isSelectItemCircle) {
            btn.layer.cornerRadius = btnH * 0.5;
            btn.layer.masksToBounds = YES;
        }
    }
    
    /** 选择按钮数组分割线 */
    UIView *btnArrDivider = self.btnArrDivider;
    btnArrDivider.alpha = 0.5;
    btnArrDivider.backgroundColor = [UIColor grayColor];
    btnArrDivider.frame = CGRectMake(0, CGRectGetMaxY(selectBtnView.frame), self.frame.size.width, 1);
    
    /** 底部按钮 */
    UIButton *bottom = self.bottomBtn;
    [bottom setTitle:self.bottomBtnTitle forState:UIControlStateNormal];
    [bottom setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [bottom setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [bottom addTarget:self action:@selector(bottomClick) forControlEvents:UIControlEventTouchUpInside];
    bottom.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    CGFloat bottomX = titleLabelX;
    CGFloat bottomY = CGRectGetMaxY(btnArrDivider.frame);
    CGFloat bottomW = self.frame.size.width;
    CGFloat bottomH = self.frame.size.height - bottomY;
    bottom.frame = CGRectMake(bottomX, bottomY, bottomW, bottomH);
}

#pragma mark - 底部按钮被点击
- (void)bottomClick
{
    if ([self.MyDelegate respondsToSelector:@selector(bottomBtnClick:)]) {
        [self.MyDelegate bottomBtnClick:self];
    }
}

#pragma mark - 条目被选择
- (void)itemSelected:(UIButton *)btn
{
    if ([self.MyDelegate respondsToSelector:@selector(itemSelected:tag:)]) {
        [self.MyDelegate itemSelected:self tag:(int)btn.tag];
    }
    
    if (self.isSelectItemCircle) {
        [self.currentBtn setBackgroundColor:[UIColor clearColor]];
        if (btn.tag == 0) { //男
            [btn setBackgroundColor:[UIColor colorWithHexString:@"#5071F3"]];
        }
        
        if (btn.tag == 1) { //女
            [btn setBackgroundColor:[UIColor colorWithHexString:@"#FA886B"]];
        }
        self.currentBtn = btn;
    }

}

- (void)btnSelected:(int)tag
{
    UIButton *btn = self.btnArr[tag];
    btn.tag = tag;
    [self itemSelected:btn];
}

@end
